//
//  HealthViewController.m
//  SHHCPhone
//
//  Created by gfsh on 14-6-23.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "HealthViewController.h"
#import "HealthViewCell.h"
#import "HealthDetailsViewController.h"
#import "HealthTableView.h"
#import "HealthViewInfo.h"
#import "HealthData.h"

@interface HealthViewController ()<AutoRefreshTableViewDelegate>
{
    HealthTableView *mainTable;
    
    BOOL isFirst;
}
@property (nonatomic,retain) NSArray *dataArray;
@end

@implementation HealthViewController

@synthesize dataArray;

- (void)viewDidLoad
{
    [super viewDidLoad];
    navigationBarView.title = @"健康动态";
    [navigationBarView setNormalLeftMenuButton];
    [navigationBarView setNormalRightButton];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if (!isFirst) {
        isFirst = YES;
        //是否存在本地数据
        if (![self getLocalData:NO]) {
            [self againLoading];
        }
    }
}

-(void)showView
{
    [self hideProgressView];
    
    mainTable = [[HealthTableView alloc] initWithFrame:CGRectMake(0, NAVIGATIONBAR_HEIGHT, CURRENT_CONTENT_WIDTH, CURRENT_CONTENT_HEIGHT-NAVIGATIONBAR_HEIGHT)];
    [mainTable setDelegate:self];
    [mainTable setSupVC:self];
    [mainTable setCurrentRefreshType:ALL_REFRESH_STATE];
    [mainTable setGetDataNumber:IOS_GET_NUMBER];
    [mainTable setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:mainTable];
}

//重新加载数据
-(void)againLoading
{
    [self showProgressView:nil belowSubview:navigationBarView];
    [self refreshTableData];
}

#pragma mark - 本地数据
-(BOOL)getLocalData:(BOOL)Failed
{
    //是否存在本地数据
    NSString *local = [self getLocalData];
    if (local) {
        NSDictionary *dic = [local objectFromJSONString];
        NSArray *newArray = [dic objectForKey:@"list"];
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
        for (NSDictionary *d in newArray) {
            HealthViewInfo *info = [[HealthViewInfo alloc] init:d];
            [array addObject:info];
        }
        if (!mainTable) {
            [self showView];
        }
        mainTable.autoDataArray = array;
        return YES;
    }else{
        if (Failed) {
            [self clickAgainLoadingView:0];
        }
        return NO;
    }
}

#pragma mark - AutoRefreshTableViewDelegate
//刷新数据
-(void)refreshTableData
{
    [self addData:nil];
}

//加载更多数据
-(void)loadingMoreTableData
{
    [self addData:((HealthViewInfo*)mainTable.autoDataArray.lastObject).id];
}

-(void)addData:(NSString*)lastId
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:1];
    [dic setValue:IOS_HEALTHDYNAMICLIST forKey:IOS_METHOD_NAME];
    [dic setValue:[NSNumber numberWithInt:IOS_GET_NUMBER] forKey:@"count"];
    if (lastId) {
        [dic setValue:lastId forKey:@"max_id"];
//        [dic setValue:[NSNumber numberWithInt:2] forKey:@"page"];
    }
    
    ASIHTTPRequest *request =  [NetUtls httpGet:self withValue:dic tag:IOS_HEALTHDYNAMICLIST_TAG need:NO];
    [self.netArray addObject:request];
    [request startAsynchronous];
}

#pragma mark - ASIHTTPRequestDelegate
-(void)requestFinished:(ASIHTTPRequest *)request
{
    CLog(@"%@",[request responseString]);
    NSDictionary *dic = [[request responseString] objectFromJSONString];
    if (dic != nil) {
        NSArray *newArray = [dic objectForKey:@"list"];
        if (newArray.count>0) {
            NSMutableArray *array = nil;
            if (mainTable.currentRefreshState == LOADING_MORE_STATE) {
                array = [NSMutableArray arrayWithArray:mainTable.autoDataArray];
            }else{
                //存储最新的数据信息
                [self saveLocalData:[request responseString]];
                array = [NSMutableArray arrayWithCapacity:1];
            }
            for (NSDictionary *d in newArray) {
                HealthViewInfo *info = [[HealthViewInfo alloc] init:d];
                [array addObject:info];
            }
            if (!mainTable) {
                [self showView];
            }
            mainTable.autoDataArray = array;
        }else{
            if (mainTable.currentRefreshState == LOADING_MORE_STATE) {
                mainTable.autoDataArray = mainTable.autoDataArray;
            }else{
                if (!mainTable) {
                    [self clickAgainLoadingView:1];
                }else{
                    [mainTable recoverShowState];
                }
            }
        }
    }else{
        [self requestFailed:request];
    }
}

-(void)requestFailed:(ASIHTTPRequest *)request
{
    if (mainTable) {
        [mainTable recoverShowState];
    }else{
//        [self clickAgainLoadingView:0];
        [self getLocalData:YES];
    }
}

@end
